<?php
/**
* エラーチェッククラス
**/
require_once( '../system/Class_M_DAO.php' );
class Class_M_COST_TVSP_MATER extends Class_M_DAO {
	//protected properties begin
	/**
	 *
	 * テーブル名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_table  = "M_COST_TVSP_MATER";
	
	/**
	 *
	 * 主キー名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_pkey   = "id";
	
	/**
	 *
	 * ソート順
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	 protected $_order = "id";
	 
	 protected $_cols  = array(
		"year4"       => array(AOKI_VARTYPE_STR),
		"mater_name"  => array(AOKI_VARTYPE_STR),
		"cm_memo"     => array(AOKI_VARTYPE_STR),
		
	 );
	
	 protected $_checks  = array(
		"mater_name"     => array( "EXIST" ),
	 );
	 
	 //maxlength 
	 protected $_maxlens  = array(
		"mater_name "   => 50 ,
		"cm_memo"       => 300
	 );
	
	public function search($where ,&$page = NULL ){
		$sql = "SELECT a.*,(select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name from {$this->_table}  a " ;
		return  parent::search($where ,$page ,$sql);
	}
	
	public function searchcsv($where  ){
		$page = NULL ;
		$sql = "SELECT a.*, c.electric_name from {$this->_table}  a" .
				" inner join M_COST_TVSP_MELEC b on a.id = b.materid  " .
				" inner join M_ELECTRIC c on b.electricid = c.id ";
		return  parent::search($where ,$page ,$sql);
	}
	
	public function getElectric($value) {
			$sql = "SELECT a.electricid,b.electric_name  FROM M_COST_TVSP_MELEC a inner join M_ELECTRIC b on a.electricid = b.id  WHERE materid = '{$value}'";
		
		try {
			$this->_db->query( $sql );
			$data = array();
			while( $row = $this->_db->fetch() ){
				$data[] = $row;
			}
			return $data;
		} catch (Exception $e) {
			echo "例外キャッチ：", $e->getMessage(), "\n";
			return NULL;
		}
	}
	
	public function getFormElectric($value ) {
		if (empty($value)) {
			$sql = "SELECT a.id,a.electric_name,'' as electricid  FROM M_ELECTRIC  a ";
		}else {
			$sql = "SELECT a.id,a.electric_name,b.electricid  FROM M_ELECTRIC  a left join M_COST_TVSP_MELEC b on b.electricid = a.id  and b.materid = '{$value}'";
		}
		
		
		try {
			$this->_db->query( $sql );
			$data = array();
			while( $row = $this->_db->fetch() ){
				$data[] = $row;
			}
			return $data;
		} catch (Exception $e) {
			echo "例外キャッチ：", $e->getMessage(), "\n";
			return NULL;
		}
	}
}

?>